﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace SmartPay.WCFServiceWebRole
{
    // NOTE: In order to launch WCF Test Client for testing this service, please select Iso8583.svc or Iso8583.svc.cs at the Solution Explorer and start debugging.
    public class Iso8583 : IIso8583
    {
        private static string configFileName = "messagemap-config.xml";
        private static string configPath = String.Empty;
        private static SmartPay.Bussiness.Iso8583 request;
        private static bool isInitialized;

        public Iso8583()
        {
            if (!isInitialized)
            {
                var found = false;
                var configPath = configFileName;
                if (System.IO.File.Exists(configPath))
                    found = true;
                else
                {
                    configPath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + configFileName;
                    if (System.IO.File.Exists(configPath))
                        found = true;
                }

                if (found)
                {
                    var fileStream = new System.Xml.XmlTextReader(configPath);
                    request = new SmartPay.Bussiness.Iso8583(configPath);
                    isInitialized = true;
                }
            }
        }
            
        public string Test()
        {
            return "This is a Hello! message from Windows Azure";
        }

        public string DoTransferInquiry()
        {
            var now = DateTime.Now;
            if (request != null)
            {
                request.DoTransferInquiry();
                var resultMessage = request.HasErrors ? request.Errors : request.ResponseModel;
                resultMessage += request.DebugInfo;
                var time = DateTime.Now - now;
                return resultMessage + String.Format("{3}[TOTAL TIME] {0}:{1}:{2}", time.Minutes, time.Seconds, time.Milliseconds, Environment.NewLine);
            }
            else
            {
                return "[DoTransferInquiry] Request is null";
            }
        }
    }
}
